package com.youlai.system.service;

import com.youlai.system.common.base.TaskInfo;
import com.youlai.system.common.enums.TaskStatusEnum;
import com.youlai.system.common.enums.TaskTypeEnum;
import com.youlai.system.common.util.ExcelUtils;
import com.youlai.system.model.entity.SysDept;
import com.youlai.system.model.entity.SysTask;
import com.youlai.system.model.vo.*;
import com.youlai.system.plugin.easyexcel.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 一键导入处理业务异步调用接口
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class ImportDataExecService {

    private final SysTaskService taskService;

    private final SysDeptService deptService;


    @Async
    public void uploadImport(Long taskId, Long deptId, Integer typeId, InputStream inputStream) {
        SysTask task = taskService.getById(taskId);
        try {
            TaskInfo taskInfo = new TaskInfo();
            if (Objects.equals(typeId, TaskTypeEnum.IMPORT_TEACHER.getValue())) {
                TeacherImportListener teacherImportListener = new TeacherImportListener(deptId);
                ExcelUtils.importExcel(inputStream, "内训师", TeacherImportVO.class, teacherImportListener);
                taskInfo = teacherImportListener.getTaskInfo();

            }
            if (Objects.equals(typeId, TaskTypeEnum.IMPORT_STUDENT.getValue())) {
                AccountSourceImportListener accountSourceImportListener = new AccountSourceImportListener(deptId);
                ExcelUtils.importExcel(inputStream, "覆盖表", AccountSourceImportVO.class, accountSourceImportListener);
                taskInfo = accountSourceImportListener.getTaskInfo();
            }
            if (Objects.equals(typeId, TaskTypeEnum.IMPORT_CATEGORY_COURSE.getValue())) {
                CourseCategoryImportListener courseCategoryImportListener = new CourseCategoryImportListener();
                ExcelUtils.importExcel(inputStream, "故障科目", CourseCategoryImportVO.class, courseCategoryImportListener);
                taskInfo = courseCategoryImportListener.getTaskInfo();
            }
            if (Objects.equals(typeId, TaskTypeEnum.IMPORT_HISTORY_SCORE.getValue())) {
                ScoreRecordImportListener scoreRecordImportListener = new ScoreRecordImportListener(deptId);
                ExcelUtils.importExcel(inputStream, "记录表", ScoreRecordImportVO.class, scoreRecordImportListener);
                taskInfo = scoreRecordImportListener.getTaskInfo();
            }

            if (Objects.equals(typeId, TaskTypeEnum.IMPORT_COURSE_PERIOD.getValue())) {
                CoursePeriodImportListener coursePeriodImportListener = new CoursePeriodImportListener();
                ExcelUtils.importExcel(inputStream, "Sheet1", CoursePeriodImportVO.class, coursePeriodImportListener);
                taskInfo = coursePeriodImportListener.getTaskInfo();
            }

            if (Objects.equals(typeId, TaskTypeEnum.IMPORT_STUDENT_GRADE.getValue())) {
                SysDept dept = deptService.getById(deptId);
                AccountSourceImportListener accountSourceImportListener = new AccountSourceImportListener(deptId);
                ExcelUtils.importExcel(inputStream, dept.getName() +"职级名单", AccountSourceImportVO.class, accountSourceImportListener);
                taskInfo = accountSourceImportListener.getTaskInfo();
            }

            task.setValidCount(taskInfo.getValidCount());
            task.setInvalidCount(taskInfo.getInvalidCount());
            task.setMsg(taskInfo.getMsg());
            task.setStatus(TaskStatusEnum.SUCCESS.getValue());

        } catch (Exception e) {
            e.printStackTrace();
            task.setStatus(TaskStatusEnum.FAILURE.getValue());
            task.setMsg(e.getMessage());
            log.error(e.getMessage());
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            task.setEndTime(LocalDateTime.now());
            taskService.updateById(task);
        }

    }
}
